#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;

const int N = 1e3 + 10,INF = 0x3f3f3f3f;
int n,m;
int edges[N][N];
bool st[N];
int dist[N];
long long ret = 0;
void dijkstra()
{
    memset(dist,0x3f,sizeof dist);
    memset(st,0,sizeof st);
    dist[1] = 0;
   priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
    q.push({0,1});
    while(q.size())
    {
        pair<int,int> t = q.top();
        q.pop();
        int w=t.first,u=t.second;
        if(st[u] == true)continue;
        st[u] = true;
        for(int i = 1; i <= n; i++)
        {
            if(edges[u][i] != INF && st[i] == false && dist[i] > dist[u] + edges[u][i])
            {
                dist[i] = dist[u] + edges[u][i];
                q.push({dist[i],i});
            }
        }

    }

    for(int i = 1; i <= n; i++)
    {
        ret += dist[i];
    }

}
int main()
{
    cin >> n >> m;
    memset(edges,0x3f,sizeof edges);
    for(int i = 0; i < m; i++)
    {
        int u,v,w;
        cin >> u >> v >> w;
        edges[u][v] = min(edges[u][v],w);
    }
    dijkstra();


    for(int i = 1; i <= n; i++)
    {
        for(int j = i+ 1; j <= n; j++)
        {
            int tem = edges[j][i];
            edges[j][i] = edges[i][j];
            edges[i][j] = tem;
        }
    }
    dijkstra();
    cout << ret << endl;
    return 0;
}